package secretary;

import includes.MD5;
import includes.Sessions;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import home.HomePage;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.Account;
import controllers.Accountability;
import controllers.Logs;
import controllers.Registered;
import controllers.StudentInformation;

public class AddStudentAccount extends WebPage {
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	private Model batchModel = new Model();
	private Model noModel = new Model();
	private Model yearLevelModel = new Model();
	private Model firstNameModel = new Model();
	private Model middleNameModel = new Model();
	private Model lastNameModel = new Model();
	private DropDownChoice yearlevel, sex;
	private Model genderModel = new Model();
	private Model birthdayModel = new Model();
	private Model addressModel = new Model();
	private Model mobileModel = new Model();
	private Model passwordModel = new Model();
	private Model fieldModel = new Model();
	private List number = Arrays.asList(new String[] { "1", "2", "3", "4" });
	private List choice = Arrays.asList(new String[] { "Male", "Female" });
	
	
	MD5 md5 = new MD5();
	
	public AddStudentAccount () throws IOException, SQLException {
		
		TextField batch = new TextField("batch", batchModel);
		batch.setRequired(true);
		
		TextField no = new TextField("no", noModel);
		batch.setRequired(true);
		
		TextField firstName = new TextField("studentFirstName", firstNameModel);
		firstName.setRequired(true);
		
		TextField middleName = new TextField("studentMiddleName", middleNameModel);
		middleName.setRequired(true);
		
		TextField lastName = new TextField("studentLastName", lastNameModel);
		lastName.setRequired(true);
		
		sex = new DropDownChoice("studentGender", genderModel, choice);
		
		yearlevel =new DropDownChoice("studentYearLevel", yearLevelModel, number);
		
		TextField birthday = new TextField("studentBirthdate", birthdayModel);
		birthday.setRequired(true);
		
		TextField address = new TextField("studentAddress", addressModel);
		address.setRequired(true);
		
		TextField mobile = new TextField("studentMobile", mobileModel);
		mobile.setRequired(true);
		
		TextField password  = new PasswordTextField("studentPassword", passwordModel);
		password.setRequired(true);
		
		TextField field = new TextField("studentField", fieldModel);
		mobile.setRequired(true);
		
		
		
		Form form = new Form("addStudentAccount")
		
		{
			public void onSubmit()
			{
				try{
					
					String batch = (String) batchModel.getObject();
					String no = (String) noModel.getObject();
					String studentId = batch + "-" + no;
					String yearLevel = (String) yearLevelModel.getObject();
					String firstName = (String) firstNameModel.getObject();
					String middleName = (String) middleNameModel.getObject();
					String lastName = (String) lastNameModel.getObject();
					String gender = (String) genderModel.getObject();
					String birthday = (String) birthdayModel.getObject();
					String address = (String) addressModel.getObject();
					String mobile = (String) mobileModel.getObject();
					String password = (String) passwordModel.getObject();
					String newPassword = md5.md5(password);
					String field = (String) fieldModel.getObject();
					
					String uploader = ((Sessions)getSession()).getDesc();
					
					Date today = new Date();
					Timestamp timestamp = new Timestamp(today.getTime());
					String ts = timestamp.toString();
					
					System.out.println("id" + studentId + "year" + yearLevel +
					"first" + firstName + "middle" + middleName + "last" + lastName +
					"gender" + gender + "birthday" + birthday + "address" + address +
					"mobile" + mobile + "password" + newPassword + "field" + field +
					"status" );
					
				    StudentInformation info = new StudentInformation();
					info.setStudent_id(studentId);
					info.setStudent_yearlvl(yearLevel);
					info.setStudent_firstName(firstName);
					info.setStudent_middleName(middleName);
					info.setStudent_lastName(lastName);
					info.setStudent_birthdate(birthday);
					info.setStudent_gender(gender);
					info.setStudent_address(address);
					info.setStudent_contNo(mobile);
					info.setStudent_hobby(field);
					info.setDate_created(ts);
					sqlMap.insert("StudentInformation.insertInformation", info);
					
					int one = 1;
					
					Account b = (Account)sqlMap.queryForObject("Account.getSemester", one);
					String sem = b.getSemester(one);
					Account a = (Account)sqlMap.queryForObject("Account.getSchoolYear", one);
					String sy = a.getSchoolYear(one);
					System.out.println("SY" + sy + "SID" + sem);
					
					Account account = new Account();
					account.setPassword(newPassword);
					account.setStudent_id(studentId);
					account.setUserid(4);
					account.setSchoolyear(sy);
					account.setSemester(sem);
					sqlMap.insert("Account.addAccount", account);
					
					Registered reg = new Registered();
					reg.setSchoolyear(sy);
					reg.setSemester(sem);
					reg.setStudent_id(studentId);
					reg.setStudent_yearlevel(yearLevel);
					reg.setStudent_name(firstName + " " + middleName + " " + lastName);
					reg.setUploader(uploader);
					reg.setDate_created(ts);
					sqlMap.insert("Registered.insert", reg);
					
					Logs log = new Logs();
					log.setLog_activity("Add Student Account:" + studentId);
					log.setLog_officer(uploader);
					log.setLog_time(ts);
					
					sqlMap.insert("Logs.insert", log);
				
					if (yearLevel == "1")
					{
					FreshmenList page = new FreshmenList();
					setResponsePage(page);
					}
					else if(yearLevel == "2")
					{
					SophiesList page = new SophiesList();
					setResponsePage(page);
						
					}
					else if(yearLevel == "3")
					{
					JuniorsList page = new JuniorsList();
					setResponsePage(page);
						
					}
					else if(yearLevel == "4")
					{
					SeniorsList page = new SeniorsList();
					setResponsePage(page);	
					}
			
				}
				
				catch (SQLException e){
				  error("Record Exists");
				  e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					error("Fill up necessary fields");
					e.printStackTrace();
				} 	
			}};
			
			add(new BookmarkablePageLink("home", SecretaryHome.class));
			add(new BookmarkablePageLink("logoutPage", HomePage.class));
			add(new BookmarkablePageLink("freshmen", FreshmenList.class));
			add(new BookmarkablePageLink("sophies", SophiesList.class));
			add(new BookmarkablePageLink("juniors", JuniorsList.class));
			add(new BookmarkablePageLink("seniors", SeniorsList.class));
			add(new BookmarkablePageLink("add", AddStudentAccount.class));
			add(new FeedbackPanel("error"));
			
			form.add(batch);
			form.add(no);
			form.add(firstName);
			form.add(middleName);
			form.add(lastName);
			form.add(yearlevel);
			form.add(sex);
			form.add(birthday);
			form.add(address);
			form.add(mobile);
			form.add(field);
			form.add(password);
			
			
			
			add(form);
			
		}
}

	
